package org.lq.er.view.subject;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.ParseException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;

import org.lq.er.entity.Subject;
import org.lq.er.service.SubjectService;
import org.lq.er.service.impl.SubjectServiceImpl;
import org.lq.er.util.AutoId;
import org.lq.er.util.CastUtil;
import org.lq.er.util.DateUtil;

import com.eltima.components.ui.DatePicker;

import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import java.awt.Font;

public class V_subject_update extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JComboBox combox_Name;
	private DatePicker dp_StartDate;
	private DatePicker dp_EndDate;
	private DatePicker dp_BeginTime;
	private JTextField txt_Duration;
	private JTextField txt_Cost;
	private Subject update_subject;
	private JComboBox comboBox_1;
	private JComboBox comboBox_2;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					V_subject_update frame = new V_subject_update();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public V_subject_update() {
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 450, 479);
		setLocationRelativeTo(null);
		contentPane = new JPanel();
		contentPane.setBackground(Color.white);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);

		JPanel panel = new JPanel();
		panel.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "\u79D1\u76EE\u4FEE\u6539", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0)));
		contentPane.add(panel, BorderLayout.CENTER);
		panel.setLayout(null);

		JLabel lbl_Name = new JLabel("科目名称:");
		lbl_Name.setBounds(47, 51, 81, 15);
		panel.add(lbl_Name);

		combox_Name = new JComboBox();
		combox_Name.setFont(new Font("Microsoft JhengHei", Font.PLAIN, 12));
		combox_Name.setModel(new DefaultComboBoxModel(new String[] {"CET-4", "CET-6"}));
		combox_Name.setBounds(148, 48, 81, 21);
		panel.add(combox_Name);

		JLabel lbl_StartDate = new JLabel("报名开始日期:");
		lbl_StartDate.setBounds(47, 89, 78, 15);
		panel.add(lbl_StartDate);

		dp_StartDate = new DatePicker();
		dp_StartDate.setBounds(146, 86, 101, 21);
		panel.add(dp_StartDate);

		JLabel lbl_EndDate = new JLabel("报名截止日期:");
		lbl_EndDate.setBounds(47, 132, 78, 15);
		panel.add(lbl_EndDate);

		dp_EndDate = new DatePicker();
		dp_EndDate.setBounds(146, 130, 101, 21);
		panel.add(dp_EndDate);

		JLabel lbl_BeginTime = new JLabel("考试开始日期:");
		lbl_BeginTime.setBounds(47, 181, 84, 15);
		panel.add(lbl_BeginTime);

		dp_BeginTime = new DatePicker();
		dp_BeginTime.setBounds(146, 176, 101, 21);
		panel.add(dp_BeginTime);

		JLabel lbl_Duration = new JLabel("考试时长:");
		lbl_Duration.setBounds(47, 256, 84, 15);
		panel.add(lbl_Duration);

		txt_Duration = new JTextField();
		txt_Duration.setColumns(10);
		txt_Duration.setBounds(146, 253, 101, 21);
		panel.add(txt_Duration);

		JLabel lbl_Cost = new JLabel("费用:");
		lbl_Cost.setBounds(48, 293, 84, 15);
		panel.add(lbl_Cost);

		txt_Cost = new JTextField();
		txt_Cost.setColumns(10);
		txt_Cost.setBounds(146, 291, 101, 21);
		panel.add(txt_Cost);
		
		JButton btn_Add = new JButton("修改");
		btn_Add.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String dateString = (dp_BeginTime.getInnerTextField().getText().toString()).substring(0, 10) + " " + comboBox_1.getSelectedItem() + ":" + comboBox_2.getSelectedItem() + ":00";
				System.out.println((dp_BeginTime.getInnerTextField().getText().toString()).substring(0, 10));
				// 如果未输入信息则进行提示,不向数据库添加
				if (dp_StartDate == null || dp_EndDate == null || dp_BeginTime == null
						|| txt_Duration == null || txt_Cost == null) {
					JOptionPane.showMessageDialog(V_subject_update.this, "请输入基本信息");
				} else {
					Subject subject = new Subject();
					subject.setId(update_subject.getId());
					subject.setSubjectName(combox_Name.getSelectedItem().toString());
					try {
						subject.setStartDate(CastUtil.parse(dp_StartDate.getText(), CastUtil.DEFAULT_DATE_PATTERN));
						subject.setEndDate(CastUtil.parse(dp_EndDate.getText(), CastUtil.DEFAULT_DATE_PATTERN));
						subject.setBeginTime(DateUtil.getDate(dateString));
					} catch (ParseException e1) {
						e1.printStackTrace();
					}
//					subject.setDetailTime(comboBox_1.getSelectedItem() + ":" + comboBox_2.getSelectedItem() + ":00");
					subject.setDuration(Integer.valueOf(txt_Duration.getText()));
					subject.setCost(Integer.valueOf(txt_Cost.getText()));
					SubjectService ss = new SubjectServiceImpl();
					if (ss.update(subject)) {
						JOptionPane.showMessageDialog(V_subject_update.this, "修改科目成功", "修改", 1);
						dispose();
					}
				}
			}
		});
		btn_Add.setBounds(47, 368, 93, 23);
		panel.add(btn_Add);
		
		JButton btn_Cancel = new JButton("取消");
		btn_Cancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
			}

		});
		btn_Cancel.setBounds(261, 368, 93, 23);
		panel.add(btn_Cancel);
		
		JLabel lblNewLabel = new JLabel("开始具体时间:");
		lblNewLabel.setBounds(47, 218, 81, 15);
		panel.add(lblNewLabel);
		
		comboBox_1 = new JComboBox();
		comboBox_1.setModel(new DefaultComboBoxModel(new String[] {"08", "09", "10", "15", "16", "17"}));
		comboBox_1.setBounds(147, 215, 58, 21);
		panel.add(comboBox_1);
		
		comboBox_2 = new JComboBox();
		comboBox_2.setModel(new DefaultComboBoxModel(new String[] {"00", "15", "30", "45"}));
		comboBox_2.setBounds(221, 215, 58, 21);
		panel.add(comboBox_2);
	}
	public  V_subject_update(int id) {
		this();
		update_subject = new SubjectServiceImpl().findById(id);
		combox_Name.setSelectedItem(update_subject.getSubjectName());
		dp_StartDate.getInnerTextField().setText(CastUtil.format(update_subject.getStartDate()));
		dp_EndDate.getInnerTextField().setText(CastUtil.format(update_subject.getEndDate()));
		dp_BeginTime.getInnerTextField().setText(DateUtil.toDate(update_subject.getBeginTime()));
		comboBox_1.setSelectedItem((DateUtil.toTime(update_subject.getBeginTime())).substring(0, 2));
		comboBox_2.setSelectedItem((DateUtil.toTime(update_subject.getBeginTime())).substring(3));
		txt_Duration.setText(String.valueOf(update_subject.getDuration()));
		txt_Cost.setText(String.valueOf(update_subject.getCost()));
		
	}
}
